package creature;

import processing.core.PApplet;
import toolib.util.ColorUtil;

public class Background {
	
	private static final int COLOUR_BLACK	= ColorUtil.rgb (8, 15, 43);
	private static final int COLOUR_DARK	= ColorUtil.rgb (9, 45, 81);
	private static final int COLOUR_BRIGHT	= ColorUtil.rgb (18, 55, 90);
	
	private static final int NOISE_STRETCH	= 90;
	
	PApplet			_applet;
	private float	_tick	= 0;
	private float	_angle	= 237;
	private int[]	_color;
	
	
	public Background (PApplet theApplet) {
		_applet = theApplet;
	}
	
	public void render () {
		int num	= (int) ((_applet.width + (int) Math.ceil (_angle) * 2) / 2);
		
		_applet.strokeWeight (2);
		
		for (int i=0; i < num; i++) {
			_applet.stroke (_color[i]);
			_applet.line (i*2 - _angle, _applet.height, i*2, 0);
		}
	}
	
	public void generate () {
		// Number of vertical lines
		float num	= (_applet.width + (int) Math.ceil (_angle) * 2) / 2;
		_color		= new int[(int) num];
		
		/* Generate rough noise pattern, number of values 
		 * is defined by the width+angle divided by the 
		 * stretch size in the rendered version.
		 */
		float[] arr = new float[(int) Math.ceil (num / NOISE_STRETCH)];
		for (int i=0; i < arr.length; i++) {
			arr[i] = _applet.noise (i, _tick);
		}
		
		int index1	= 0;
		int index2	= 1;
		int c1		= ColorUtil.lerpRGB (COLOUR_DARK, COLOUR_BRIGHT, arr[0]);
		int c2		= ColorUtil.lerpRGB (COLOUR_DARK, COLOUR_BRIGHT, arr[1]);
		
		
		
		for (int i=0; i < num; i++) {
			
			if (index1 != (int) Math.floor (i / NOISE_STRETCH)) {
				index1 = (int) Math.floor (i / (float) NOISE_STRETCH);
				index2 = index1 + 1;
				
				c1 = c2;
				if (index2 < arr.length) {
					if (arr[index2] < 0.8) {
						c2 = ColorUtil.lerpRGB (COLOUR_BLACK, COLOUR_DARK, arr[index2]);
					}else{
						c2 = ColorUtil.lerpRGB (COLOUR_DARK, COLOUR_BRIGHT, arr[index2]);
					}
				}
			}
			float prog = (i % NOISE_STRETCH) / (float) NOISE_STRETCH;
			_color[i] = ColorUtil.lerpRGB (c1, c2, prog);	
		}
		
		_tick += 0.026;
	}
}